package com.hxb.notificationdemo.notification;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.provider.MediaStore;

import androidx.annotation.RequiresApi;

import com.google.gson.Gson;
import com.hxb.notificationdemo.BaseApplication;
import com.hxb.notificationdemo.R;


/**
 * 状态栏Notification工具类
 */
public class StatusBarNotificationUtil {

    private static final String CHANNEL_ID = BaseApplication.getContext().getPackageName();//通道id
    private static final CharSequence CHANNEL_NAME = "会话类型";//通道名称
    private static PriorityEnum priorityEnum = PriorityEnum.DEFAULT;//通知重要性

    public enum PriorityEnum {
        DEFAULT,        //默认
        HIGH,           //高：发出声音
        LOW,            //低：不发出声音，也不在状态栏中显示
        UNSPECIFIED,    //紧急：发出声音并以浮动通知的形式显示
        MIN,           //中：不发出声音
        NONE            //不重要的通知:不显示在阴影中
    }

    /**
     * 设置通知重要性
     */
    public void setNotificationPriority(PriorityEnum priorityEnum) {
        this.priorityEnum = priorityEnum;
    }

    private int getNotificationEnum(PriorityEnum priorityEnum) {
        int priority;
        switch (priorityEnum) {
            case LOW:
                priority = NotificationManager.IMPORTANCE_LOW;
                break;
            case MIN:
                priority = NotificationManager.IMPORTANCE_MIN;
                break;
            case HIGH:
                priority = NotificationManager.IMPORTANCE_HIGH;
                break;
            case NONE:
                priority = NotificationManager.IMPORTANCE_NONE;
                break;
            case DEFAULT:
                priority = NotificationManager.IMPORTANCE_DEFAULT;
                break;
            case UNSPECIFIED:
//                priority = NotificationManager.IMPORTANCE_UNSPECIFIED;
//                break;
            default:
                priority = NotificationManager.IMPORTANCE_DEFAULT;
                break;
        }
        return priority;
    }


    /**
     * 开始通知
     *
     * @param context          页面上下文
     * @param title            通知栏标题
     * @param content          通知栏内容
     * @param idIco            通知栏图片id
     * @param NotificationCode 通知code
     * @param AutoCancel       通知栏是否可点击消除
     * @param Ongoing          通知栏是否可清除
     * @param data             参数
     */
    public void showNotification(Context context,
                                 String title,
                                 String content,
                                 int idIco,
                                 int NotificationCode,
                                 boolean AutoCancel,
                                 boolean Ongoing,
                                 NotificationData data) {

        Context ApplicationContext = BaseApplication.getContext();

        long[] vibrate = {0, 500, 1000, 1500};
        PendingIntent pendingIntent = null;
        NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification.Builder notification;
        if (data != null) {

            Intent intent = setNotificationIntent(NotificationCode, data, ApplicationContext);
            pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

            // 此处必须兼容android O设备，否则系统版本在O以上可能不展示通知栏
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                notification = NotificationBuilderByO(title, content, idIco, AutoCancel, Ongoing, ApplicationContext, vibrate, pendingIntent, manager);
            } else {
                notification = new Notification
                        .Builder(ApplicationContext)
                        .setAutoCancel(AutoCancel)
                        .setOngoing(Ongoing)
                        .setContentTitle(title)
                        .setContentText(content)
                        .setWhen(System.currentTimeMillis())
                        .setSmallIcon(idIco)
                        .setLargeIcon(BitmapFactory.decodeResource(ApplicationContext.getResources(), idIco))
                        .setVibrate(vibrate)
                        .setContentIntent(pendingIntent)
//                        .setDeleteIntent(pendingIntentCancel)
//                        .setPriority(NotificationCompat.PRIORITY_HIGH)
                        .setDefaults(Notification.DEFAULT_ALL)//使用默认的声音、振动、闪光
                        .setSound(MediaStore.Audio.Media.INTERNAL_CONTENT_URI);

            }

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                notification.setGroupSummary(false)
                        .setGroup("group");
            }
            //NotificationCode决定了是否覆盖消息,相同的update，不同add
            manager.notify(NotificationCode, notification.build());
        }

    }

    /**
     * 设置Intent
     *
     * @param NotificationCode
     * @param data
     * @param applicationContext
     * @return
     */
    private Intent setNotificationIntent(int NotificationCode, NotificationData data, Context applicationContext) {
        Intent intent = new Intent(applicationContext, NotificationBroadcastReceiver.class);
        intent.setAction(data.getAction());
        if (data.getAction().equals(NotificationAction.jump)) {
            intent.putExtra("Data", new Gson().toJson(data));
        }
        intent.putExtra(NotificationBroadcastReceiver.TYPE, NotificationCode);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        return intent;
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    private Notification.Builder NotificationBuilderByO(String title,
                                                        String content,
                                                        int idIco,
                                                        boolean AutoCancel,
                                                        boolean Ongoing,
                                                        Context applicationContext,
                                                        long[] vibrate,
                                                        PendingIntent pendingIntent,
                                                        NotificationManager manager) {
        Notification.Builder notification;
        notification = new Notification
                .Builder(BaseApplication.getContext(), CHANNEL_ID)
                .setAutoCancel(AutoCancel)
                .setOngoing(Ongoing)
                .setContentTitle(title)
                .setContentText(content)
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(applicationContext.getResources(), idIco))
                .setContentIntent(pendingIntent)
//                .setDeleteIntent(pendingIntent)
//              .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setChannelId(BaseApplication.getContext().getPackageName());

        NotificationChannel channel = new NotificationChannel(
                CHANNEL_ID,
                CHANNEL_NAME,//这块Android9.0分类的比较完整，你创建多个这样的东西，你可以在设置里边显示那个或者第几个
                getNotificationEnum(priorityEnum)
        );
        channel.setDescription("通知");
        channel.enableLights(true);
        channel.setLightColor(Color.RED);
        channel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
        manager.createNotificationChannel(channel);
        return notification;
    }


}
